package com.spic.business.train.trainUser.controller;

import com.spic.business.common.core.utils.poi.BusinessExcelUtil;
import com.spic.business.train.trainUser.domain.TrainUser;
import com.spic.business.train.trainUser.service.ITrainUserService;
import com.spic.common.core.domain.R;
import com.spic.common.core.utils.poi.ExcelUtil;
import com.spic.common.core.web.controller.BaseController;
import com.spic.common.core.web.domain.AjaxResult;
import com.spic.common.core.web.page.TableDataInfo;
import com.spic.common.log.annotation.Log;
import com.spic.common.log.enums.BusinessType;
import com.spic.common.security.annotation.PreAuthorize;
import com.spic.system.api.RemoteUserService;
import com.spic.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 培训用户Controller
 *
 * @author spic
 * @date 2021-10-15
 */
@RestController
@RequestMapping("/train/trainUser")
public class TrainUserController extends BaseController
{
    @Autowired
    private ITrainUserService trainUserService;
    @Autowired
    private RemoteUserService remoteUserService;

    /**
     * 查询培训用户列表
     */

    @GetMapping("/list")
    public TableDataInfo list(TrainUser trainUser)
    {
        startPage();
        List<TrainUser> list = trainUserService.selectTrainUserList(trainUser);
        return getDataTable(list);
    }

    @GetMapping("/getUsers")
    public AjaxResult getUsers(SysUser user){
        R<List<SysUser>> list = remoteUserService.getSysUserByDeptId(user.getDeptId());
        List<SysUser> data = list.getData();
        List<SysUser> filterList = new ArrayList<>();
        for (int i = 0; i < data.size(); i++) {
            SysUser sysUser = data.get(i);
            TrainUser trainUser = new TrainUser();
            trainUser.setUserId(sysUser.getUserId());
            List<TrainUser> trainUsers = trainUserService.selectTrainUserList(trainUser);
            if(trainUsers.size() >= 3) {
                filterList.add(sysUser);
            }
        }
        return AjaxResult.success(filterList);
    }

    /**
     * 导出培训用户列表
     */
    @PreAuthorize(hasPermi = "business/train:trainUser:export")
    @Log(title = "培训用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TrainUser trainUser) throws IOException
    {
        List<TrainUser> list = trainUserService.selectTrainUserList(trainUser);
        ExcelUtil<TrainUser> util = new BusinessExcelUtil<TrainUser>(TrainUser.class);
        util.exportExcel(response, list, "培训用户数据");
    }

    /**
     * 获取培训用户详细信息
     */
    @PreAuthorize(hasPermi = "business/train:trainUser:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(trainUserService.selectTrainUserById(id));
    }

    /**
     * 新增培训用户
     */
    @PreAuthorize(hasPermi = "business/train:trainUser:add")
    @Log(title = "培训用户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TrainUser trainUser)
    {
        return toAjax(trainUserService.insertTrainUser(trainUser));
    }
    /**
     * 批量新增培训用户
     */
    @PreAuthorize(hasPermi = "business/train:trainUser:add")

    @PostMapping(value = "adds")
    public AjaxResult adds(@RequestBody List<TrainUser> trainUsers)
    {
        return toAjax(trainUserService.insertTrainUsers(trainUsers));
    }
    /**
     * 修改培训用户
     */
    @PreAuthorize(hasPermi = "business/train:trainUser:edit")
    @Log(title = "培训用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TrainUser trainUser)
    {
        return toAjax(trainUserService.updateTrainUser(trainUser));
    }

    /**
     * 删除培训用户
     */
    @PreAuthorize(hasPermi = "business/train:trainUser:remove")
    @Log(title = "培训用户", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(trainUserService.deleteTrainUserByIds(ids));
    }


}
